package base.thread_JUC2.chapter04.demo05;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// 可延时执行的执行器
public class TestCase {
    public static void main(String[] args) {
        // 可延时调度的线程池，此处池中只包含 1 个线程
        ScheduledThreadPoolExecutor pool = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1);

        System.out.printf("Main: Starting at: %s\n",new Date());
        for (int i = 0; i < 5; i++) {
            Task task = new Task("Task_" + i);
            // 等待i时间后开始执行任务
            pool.schedule(task, i, TimeUnit.SECONDS);
        }
        pool.shutdown();

        try {
            // 等待所有任务结束
            boolean b = pool.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.printf("Main: Ends at: %s\n",new Date());
    }
}
